<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import $api from '@/api/api'
import { Search, User, CreditCard } from '@element-plus/icons-vue'

// 搜索表单数据
const searchForm = reactive({
  sno: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 获取借书证列表
const fetchLibCards = async () => {
  loading.value = true
  try {
    const res = await $api.getLibCardList(searchForm)
    if (res.code === 1) {
      tableData.value = res.data
    } else {
      ElMessage.error(res.msg || '获取借书证列表失败')
    }
  } catch (error) {
    ElMessage.error('获取借书证列表失败')
  } finally {
    loading.value = false
  }
}

// 处理搜索
const handleSearch = () => {
  fetchLibCards()
}

// 重置搜索
const handleReset = () => {
  searchForm.sno = ''
  handleSearch()
}

// 获取操作类型显示文本和标签类型
const getOpTypeInfo = (opType) => {
  const typeMap = {
    'LOSS': {
      text: '挂失',
      type: 'warning'
    },
    'CANCEL_LOSS': {
      text: '取消挂失',
      type: 'success'
    },
    'NEW': {
      text: '新办',
      type: 'primary'
    },
    'RENEW': {
      text: '续期',
      type: 'info'
    }
  }
  return typeMap[opType] || { text: opType, type: '' }
}

onMounted(() => {
  fetchLibCards()
})
</script>

<template>
  <div class="student-manage-page">
    <div class="background-decoration">
      <div class="floating-shape shape-1"></div>
      <div class="floating-shape shape-2"></div>
      <div class="floating-shape shape-3"></div>
    </div>

    <!-- 搜索区域 -->
    <el-card class="search-card" shadow="never">
      <div class="search-header">
        <div class="header-content">
          <div class="icon-wrapper">
            <el-icon class="header-icon"><CreditCard /></el-icon>
          </div>
          <h2>借书证管理</h2>
          <p class="header-subtitle">管理学生借书证信息</p>
        </div>
      </div>
      <el-form :model="searchForm" inline class="search-form">
        <el-form-item label="学号">
          <el-input
              v-model="searchForm.sno"
              placeholder="请输入学号"
              clearable
              class="custom-input"
              :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              @click="handleSearch"
              :icon="Search"
              class="search-btn"
          >
            搜索
          </el-button>
          <el-button @click="handleReset" class="reset-btn">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 借书证列表 -->
    <el-card class="list-card" shadow="never">
      <el-table
          :data="tableData"
          v-loading="loading"
          border
          stripe
          style="width: 100%"
          class="custom-table"
      >
        <el-table-column prop="serNum" label="流水号" width="100" />
        <el-table-column prop="sno" label="学号" min-width="120" />
        <el-table-column prop="originCardNo" label="原卡号" min-width="120" />
        <el-table-column prop="newCardNo" label="新卡号" min-width="120" />
        <el-table-column label="操作类型" width="100">
          <template #default="{ row }">
            <el-tag :type="getOpTypeInfo(row.opType).type" class="custom-tag">
              {{ getOpTypeInfo(row.opType).text }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<style scoped>
.student-manage-page {
  padding: 20px;
  min-height: 100vh;
  background: linear-gradient(120deg, #e4ecfb 0%, #f5f7fa 100%);
  position: relative;
  overflow: hidden;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.floating-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 80px;
  height: 80px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.shape-2 {
  width: 120px;
  height: 120px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.shape-3 {
  width: 60px;
  height: 60px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.search-card {
  margin-bottom: 20px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;
}

.search-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.search-header {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 25px;
}

.header-content {
  text-align: center;
}

.icon-wrapper {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  margin-bottom: 15px;
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.header-icon {
  font-size: 24px;
  color: white;
}

.search-header h2 {
  margin: 0 0 8px 0;
  font-size: 1.8em;
  color: #2c3e50;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.header-subtitle {
  margin: 0;
  color: #7f8c8d;
  font-size: 0.95em;
  font-weight: 400;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  padding: 15px 0;
  justify-content: center;
}

.custom-input {
  transition: all 0.3s ease;
}

.custom-input:hover {
  transform: translateY(-2px);
}

.search-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.search-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
}

.reset-btn {
  border: 2px solid #e1e8ed;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  color: #7f8c8d;
  transition: all 0.3s ease;
  background: white;
}

.reset-btn:hover {
  border-color: #667eea;
  color: #667eea;
  transform: translateY(-2px);
}

.list-card {
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;
}

.list-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.custom-table {
  border-radius: 12px;
  overflow: hidden;
}

.custom-tag {
  border-radius: 6px;
  font-weight: 500;
  padding: 4px 8px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  border: 2px solid #e1e8ed;
  transition: all 0.3s ease;
  background: white;
}

:deep(.el-input__wrapper:hover) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

:deep(.el-form-item__label) {
  font-weight: 600;
  color: #2c3e50;
  font-size: 0.95em;
}

:deep(.el-card__header) {
  border-bottom: none;
  padding: 20px 20px 0;
}

:deep(.el-card__body) {
  padding: 20px;
}

:deep(.el-table) {
  border-radius: 12px;
  overflow: hidden;
}

:deep(.el-table th) {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  color: #2c3e50;
  font-weight: 600;
  border-bottom: 2px solid #e9ecef;
}

:deep(.el-table td) {
  border-bottom: 1px solid #f1f3f4;
}

:deep(.el-table--striped .el-table__body tr.el-table__row--striped td) {
  background: #fafbfc;
}

:deep(.el-table--striped .el-table__body tr.el-table__row--striped:hover td) {
  background: #f0f2f5;
}

:deep(.el-table__body tr:hover td) {
  background: #f0f2f5;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .student-manage-page {
    padding: 10px;
  }

  .search-card,
  .list-card {
    border-radius: 15px;
  }

  .search-form {
    flex-direction: column;
    align-items: center;
  }

  .search-form .el-form-item {
    margin-right: 0;
    width: 100%;
    max-width: 300px;
  }

  .header-icon {
    font-size: 20px;
  }

  .search-header h2 {
    font-size: 1.5em;
  }

  :deep(.el-card__body) {
    padding: 15px;
  }
}

@media (max-width: 480px) {
  .icon-wrapper {
    width: 50px;
    height: 50px;
  }

  .header-icon {
    font-size: 18px;
  }

  .search-header h2 {
    font-size: 1.3em;
  }
}
</style>
